package com.ktwlrj.core.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * Request处理工具类
 *
 * @author zhouxuanhong
 * @date 2019-04-03 14:11
 * @since JDK1.8
 */
public class RequestUtils {
    private static final String BEARER_TYPE = "Bearer";
    private static final String ACCESS_TOKEN = "access_token";
    private static final String SESSION = "SESSION";


    /**
     * 获取登录用户SESSION
     *
     * @param request ${@link HttpServletRequest}
     * @return String ${@link String}
     * @author zhouxuanhong
     * @date 2019-04-17 17:02
     */
    public static String getCookie(HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        String value = "";
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (SESSION.equals(cookie.getName())) {
                    value = cookie.getValue();
                }
            }
        }
        return value;
    }


    /**
     * 获取登录用户SESSION
     *
     * @return String ${@link String}
     * @author zhouxuanhong
     * @date 2019-05-25 11:54
     */
    public static String getCookie() {
        HttpServletRequest request = ServletUtils.getRequest();
        if (request == null) {
            return null;
        }
        return getCookie(request);
    }

    /**
     * 获取当前请求头中token
     *
     * @param request ${@link HttpServletRequest}
     * @return String ${@link String}
     * @author zhouxuanhong
     * @date 2019-05-25 11:46
     */
    private static String getHeaderToken(HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        Enumeration<String> headers = request.getHeaders("Authorization");
        while (headers.hasMoreElements()) {
            String value = headers.nextElement();
            if ((value.toLowerCase().startsWith(BEARER_TYPE.toLowerCase()))) {
                String authHeaderValue = value.substring(BEARER_TYPE.length()).trim();
                int commaIndex = authHeaderValue.indexOf(',');
                if (commaIndex > 0) {
                    authHeaderValue = authHeaderValue.substring(0, commaIndex);
                }
                return authHeaderValue;
            }
        }
        return null;
    }


    /**
     * 获取当前请求参数中的token
     *
     * @param request ${@link HttpServletRequest}
     * @return String ${@link String}
     * @author zhouxuanhong
     * @date 2019-05-25 11:47
     */
    private static String getParameterToken(HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        return request.getParameter(ACCESS_TOKEN);
    }


    /**
     * 获取当前请求token
     *
     * @param request ${@link HttpServletRequest}
     * @return String ${@link String}
     * @author zhouxuanhong
     * @date 2019-05-25 11:47
     */
    public static String getToken(HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        String token = getHeaderToken(request);
        if (StringUtils.isBlank(token)) {
            token = getParameterToken(request);
        }
        return token;
    }


    /**
     * 获取当前请求token
     *
     * @return String ${@link String}
     * @author zhouxuanhong
     * @date 2019-05-25 11:54
     */
    public static String getToken() {
        HttpServletRequest request = ServletUtils.getRequest();
        if (request == null) {
            return null;
        }
        return getToken(request);
    }


    /**
     * 判断当前请求为ajax请求
     *
     * @param request ${@link HttpServletRequest}
     * @return Boolean ${@link Boolean}
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        if (request == null) {
            return false;
        }
        return request.getHeader("accept").contains("application/json") || (request
                .getHeader("X-Requested-With") != null && request.getHeader(
                "X-Requested-With").contains("XMLHttpRequest"));
    }
}
